import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:hm_show_flutter/api/api.dart';
import 'package:hm_show_flutter/constant/string.dart';
import 'package:hm_show_flutter/entity/activity_detail_entity.dart';
import 'package:hm_show_flutter/entity/activity_list_entity.dart';
import 'package:hm_show_flutter/entity/base_response_entity.dart';
import 'package:hm_show_flutter/entity/goods_detail_entity.dart';
import 'package:hm_show_flutter/entity/goods_list_entity.dart';
import 'package:hm_show_flutter/utils/http_util.dart';

typedef OnFail(String message);

typedef OnSuccess<T>(T successData);

class ActivityService {
  Future getActivityDetailData(int id, OnSuccess onSuccess, OnFail onFail) async {
    try {
      Map<String, dynamic> params = new FormData.from({
        "id": id,
      });
      var response =
          await HttpUtil.instance.get(Api.ACTIVITY_INFO, parameters: params);
      if (response['errno'] == 0) {
        ActivityDetailEntity entity = ActivityDetailEntity.fromJson(response);
        onSuccess(entity);
      } else {
        onFail(response['errmsg']);
      }
    } catch (e) {
      print(e);
      onFail(Strings.SERVER_EXCEPTION);
    }
  }

  Future getActivityListData(int page, OnSuccess onSuccess,
      {OnFail onFail}) async {
    try {
      FormData data = new FormData();

      data.add("pageNumber", page);
      String companyId;
      if (companyId != null) {
        data.add("companyId", companyId);
      }

      var response =
          await HttpUtil.instance.get(Api.ACTIVITY_LIST, parameters: data);
      if (response['errno'] == 0) {
        ActivityListEntity entity = ActivityListEntity.fromJson(response);
        onSuccess(entity);
      } else {
        onFail(response['errmsg']);
      }
    } catch (e) {
      print(e);
      onFail(Strings.SERVER_EXCEPTION);
    }
  }

}
